var   TREEOBJ  ="";
home ={
	menuTree:function(){
		var menuTree = Ext.create('Ext.tree.Panel', {
					id:'menuTree',
					name:'menuTree',
	                rootVisible: false,
	                root:{
						text:'Root',
						id:'0',
						expanded:true
					},
					listeners:{
						itemclick:function(){
							var args = Array.prototype.slice.call(arguments, 0);
							home.itemClick.apply(this, args);
						},
						itemcontextmenu:function(tree,record,item,index,e,obj){
							  var nodemenu=new Ext.menu.Menu({
							  		floating:true,
								     items:[{
								      text:"删除快捷菜单",
								      iconCls:'icon-delete-row',
								      handler:function(){
								      
								      }
								     }]
							    });
							    
							nodemenu.showAt(e.getXY());
						}
					}
	            });
		return menuTree;
	},
	initMenu:function(){
		var query = new Query('SysUserRoleLoadHandler', 'SysUserRoleDataSetHandler');
		query.setHandlerMethod("queryUserMemu");
		function completedFun(isSuccess,ds,response, opts){
			if(isSuccess){
				TREEOBJ = ds.getValue("tree");
				 home.treeObj =  Ext.JSON.decode(TREEOBJ);
					for ( var i = 0; i < home.treeObj.length; i++) {
						var line='';
						var id = home.treeObj[i].id;
						var tag ='<li><a id="' +id+ '"  class="active"  onclick=\"javascript:home.changeMenu(this,'+ i + ')\"><span>' + home.treeObj[i].text + '</span></a></li>';
						if(i!=0){
							 line = '<li class="menu_line"></li>';
							 tag = '<li><a id="'+ id +'" onclick=\"javascript:home.changeMenu(this,'+ i + ')\"><span>'+ home.treeObj[i].text +'</span></a></li>';
						}
						Ext.fly('menubar').insertHtml('beforeEnd', line + tag);
					}
					var menuTree = Ext.getCmp('menuTree');
					var rootNode = menuTree.getRootNode();
					rootNode.removeAll();
					menuTree.setRootNode(home.treeObj[0]);
			}
		}
		query.setCompletedFun(completedFun);
		var ds = query.load();
	},
	changeMenu:function(dom,num) {
			var id = dom.id;
			var treeObj = Ext.JSON.decode(TREEOBJ);
			for ( var i = 0; i < treeObj.length; i++) {
				if(treeObj[i].id==id){
					document.getElementById(treeObj[i].id).className="active";
				}else{
					document.getElementById(treeObj[i].id).className='';
				}
			}
			function buildTree(treeObj){
				var children = {};
			    Ext.apply(children,treeObj[num]);
				var menuPanel = Ext.getCmp('menuPanel');
				if(Ext.getCmp(menuTree)){
					Ext.getCmp(menuTree).destroy();
				}
				menuPanel.removeAll();
				var menuTree = Ext.create('Ext.tree.Panel', {
					id:'menuTree',
					name:'menuTree',
	                rootVisible: false,
					listeners:{
						itemclick:function(){
							var args = Array.prototype.slice.call(arguments, 0);
							home.itemClick.apply(this, args);
						},
						itemcontextmenu:function(tree,record,item,index,e,obj){
							  var nodemenu=new Ext.menu.Menu({
							  		floating:true,
								     items:[{
								      text:"展开",
								      iconCls:'icon-expand-all',
								      handler:function(){
								      }
								     },{
								      text:"收缩",
								      iconCls:'icon-collapse-all',
								      handler:function(){
								       alert("编辑"); 
								      }
								     },{
								      text:"加入个人工作台",
								      iconCls:'icon-add-row',
								      handler:function(){
								      
								      }
								     }]
							    });
							    
							nodemenu.showAt(e.getXY());
						}
					}
	            });
				menuTree.setRootNode(children);
				menuPanel.add(menuTree);
			}
			return buildTree(treeObj);
			
		},
	addTab:function(record){
		var menuId = record.data.id;
		var title = record.data.text;
		var url = record.data.hrefTarget;
		if( Ext.isEmpty(url) ){
			return ;
		}		
		MainTab.addTab(menuId,title,url);
	},
	itemClick:function(target,record,index,e,eOpts){
		home.addTab(record);
	},
	showLock:function(){
			var userName = Pearlsen.Utils.base64.decode(Pearlsen.Utils.cookie.get(Pearlsen.Constant.PEARLSOFT_USERCODE));
			Pearlsen.Utils.cookie.set(Pearlsen.Constant.USER_STATE,Pearlsen.Utils.base64.encode(Pearlsen.Constant.USER_STATE_LOCK));
			var lockForm = Ext.create('Pearlsen.form.PPanel',{
				handlerName:'SysUserLoadHandler',
				datasetName:'SysUserDataSetHandler',
				handlerMethod:'doLogin',
				items:[{
			            fieldLabel:'密码',
			            id:'l_password',
			            name: 'l_password',
			            inputType : "password",
			            xtype:'textfield',
			            allowBlank:false
	        	  },{
	        		  xtype:'hiddenfield',
	        		  id:'l_usercode',
			          name: 'l_usercode',
			          value:userName 
	        	  }]
			});
			var lockWin = Ext.create('widget.window',{
				layout:'fit',
				modal:true,
				closable:false,
				title:'请输入密码后解锁',
				items:lockForm,
				buttons:[{
					text:'解锁',
					handler:function(){
							var _username = Ext.getCmp("l_usercode").value;
							var _password = Ext.getCmp("l_password").value;
							var query=new Query('SysUserLoadHandler','SysUserDataSetHandler');
							query.setHandlerMethod("doLogin");
							query.addCondition("user_name",_username);
							query.addCondition("user_password",_password);
							var ds=query.load();
							if(ds==null||ds.size()==0){
								Pearlsen.Utils.showErrorMsg("解锁失败");
							}else{
								ds.next();
								if(ds.getValue("status")=='success'){
									lockWin.destroy();
								}
								else{
									Pearlsen.Utils.showErrorMsg("解锁失败");
								}
							}
						
					
					}
				}]
			}).show();
			
	},
	doLogout:function(){
		var query=new Query('SysUserLoadHandler','SysUserDataSetHandler');
		query.setHandlerMethod("doLogout");
		var ds=query.load();
		window.location=Pls.Webpath+"/index.jsp";
	}
	
};
